package com.taxshare.seller.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.response.beans.Response;
import com.taxshare.seller.constant.CodeConstant;
import com.taxshare.seller.mapper.CountGatherMapper;
import com.taxshare.seller.remote.ManagerClientService;
import com.taxshare.seller.util.BigDecimalUtils;
import com.taxshare.seller.view.dto.MonthGatherDTO;
import com.taxshare.seller.view.vo.MonthGatherResultVO;
import com.taxshare.seller.view.vo.MonthGatherVO;
import com.taxshare.seller.view.vo.OpenInvoiceGatherVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: CountGatherService
 * @Description:
 * @author: Hai
 * @date: 2021/6/25 10:59
 */
@Service
@Slf4j
public class CountGatherService extends ServiceImpl<CountGatherMapper, MonthGatherDTO> {

    @Autowired
    ManagerClientService managerClientService;

    @Autowired
    CommService commService;

    /**
     * 月度汇总统计
     *
     * @param query
     * @return
     */
    public Response getMonthGather(MonthGatherDTO query) {
        //query.setSellerTaxNoList((List)managerClientService.getResources(UserInfoUtil.getUserID(),UserInfoUtil.getUserType()).getData());
        if (StrUtil.isBlank(query.getSellerTaxNo())){
            List<String> taxNos = commService.getTaxNumbers();
            query.setSellerTaxNoList(taxNos);
        }
        MonthGatherVO vo = new MonthGatherVO();
        query.setInvoiceType(CodeConstant.INVOICETYPE_ZP);
        MonthGatherResultVO resultZP = baseMapper.getMonthGather(query);
        query.setInvoiceType(CodeConstant.INVOICETYPE_PP);
        MonthGatherResultVO resultPP = baseMapper.getMonthGather(query);
        query.setInvoiceType(CodeConstant.INVOICETYPE_DZFP);
        MonthGatherResultVO resultDZFP = baseMapper.getMonthGather(query);
        query.setInvoiceType(CodeConstant.INVOICETYPE_JDC);
        MonthGatherResultVO resultJDCFP = baseMapper.getMonthGather(query);
        //专票
        vo.setZpCount(resultZP.getCount());
        if (resultZP.getTaxSum() != null) {
            vo.setZpTaxSum(BigDecimalUtils.format(BigDecimalUtils.divide(resultZP.getTaxSum(), 1),2));
        }
        if (resultZP.getTotalSum() != null) {
            vo.setZpTotalSum(BigDecimalUtils.format(BigDecimalUtils.divide(resultZP.getTotalSum(), 1),2));
        }
        //普票
        vo.setPpCount(resultPP.getCount());
        if (resultPP.getTaxSum() != null) {
            vo.setPpTaxSum(BigDecimalUtils.format(BigDecimalUtils.divide(resultPP.getTaxSum(), 1),2));
        }
        if (resultPP.getTotalSum() != null) {
            vo.setPpTotalSum(BigDecimalUtils.format(BigDecimalUtils.divide(resultPP.getTotalSum(), 1),2));
        }
        //电子发票
        vo.setDpCount(resultDZFP.getCount());
        if (resultDZFP.getTaxSum() != null) {
            vo.setDpTaxSum(BigDecimalUtils.format(BigDecimalUtils.divide(resultDZFP.getTaxSum(), 1),2));
        }
        if (resultDZFP.getTotalSum() != null) {
            vo.setDpTotalSum(BigDecimalUtils.format(BigDecimalUtils.divide(resultDZFP.getTotalSum(), 1),2));
        }
        //机动车发票
        vo.setJdcCount(resultJDCFP.getCount());
        if (resultJDCFP.getTaxSum() != null) {
            vo.setJdcTaxSum(BigDecimalUtils.format(BigDecimalUtils.divide(resultJDCFP.getTaxSum(), 1),2));
        }
        if (resultJDCFP.getTotalSum() != null) {
            vo.setJdcTotalSum(BigDecimalUtils.format(BigDecimalUtils.divide(resultJDCFP.getTotalSum(), 1),2));
        }
        return Response.success(vo);
    }

    /**
     * 已开发票统计
     *
     * @param query
     * @return
     */
    public Response getOpenInvoiceGather(MonthGatherDTO query) {
        //query.setSellerTaxNoList((List)managerClientService.getResources(UserInfoUtil.getUserID(),UserInfoUtil.getUserType()).getData());
        if (StrUtil.isBlank(query.getSellerTaxNo())){
            List<String> taxNos = commService.getTaxNumbers();
            query.setSellerTaxNoList(taxNos);
        }
        List<OpenInvoiceGatherVO> voList = baseMapper.getOpenInvoiceGather(query);
        for(OpenInvoiceGatherVO vo:voList){
            if(vo.getBlueInvoicePrice() != null){
                vo.setBlueInvoicePrice(BigDecimalUtils.format(BigDecimalUtils.divide(vo.getBlueInvoicePrice(), 1),2));
            }
            if(vo.getBlueInvoiceTax() != null){
                vo.setBlueInvoiceTax(BigDecimalUtils.format(BigDecimalUtils.divide(vo.getBlueInvoiceTax(), 1),2));
            }
            //负废发票
            if(vo.getLoseInvoicePrice() != null){
                vo.setLoseInvoicePrice(BigDecimalUtils.format(BigDecimalUtils.divide(vo.getLoseInvoicePrice(), 1),2));
            }
            if(vo.getLoseInvoiceTax() != null){
                vo.setLoseInvoiceTax(BigDecimalUtils.format(BigDecimalUtils.divide(vo.getLoseInvoiceTax(), 1),2));
            }
            //已开正红发票
            if(vo.getRedInvoicePrice() != null){
                vo.setRedInvoicePrice(BigDecimalUtils.format(BigDecimalUtils.divide(vo.getRedInvoicePrice(), 1),2));
            }
            if(vo.getRedInvoiceTax() != null){
                vo.setRedInvoiceTax(BigDecimalUtils.format(BigDecimalUtils.divide(vo.getRedInvoiceTax(), 1),2));
            }
            //已开正废发票
            if(vo.getWasteInvoicePrice() != null){
                vo.setWasteInvoicePrice(BigDecimalUtils.format(BigDecimalUtils.divide(vo.getWasteInvoicePrice(), 1),2));
            }
            if(vo.getWasteInvoiceTax() != null){
                vo.setWasteInvoiceTax(BigDecimalUtils.format(BigDecimalUtils.divide(vo.getWasteInvoiceTax(), 1),2));
            }
        }
        return Response.success(voList);
    }



}
